package com.google.project.client.HumanResources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Sharing.ChangePassword;
import com.google.project.shared.UsersDTO;

public class HMAction extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button btnStaff;
	@UiField Button btnProject;
	@UiField Button btnAddStaff;
	@UiField Button btnSpecialize;
	@UiField Button btnMess;
	@UiField Button btnGeneralInfo;
	@UiField Label lblFullName;
	@UiField Hyperlink hypSingOut;
	@UiField Button btnHmRequire;
	@UiField Hyperlink btnChangePass;
	
	interface Binder extends UiBinder<Widget, HMAction> {
	}

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);
	UsersDTO userDTO=new UsersDTO();
	public HMAction() {
		initWidget(binder.createAndBindUi(this));
		getUserByID();
	}

	@UiHandler("btnStaff")
	void onBtnStaffClick(ClickEvent event) {
		HMEntrypoint.get().HMStaffList();
	}
	@UiHandler("btnProject")
	void onBtnProjectClick(ClickEvent event) {
		HMEntrypoint.get().HMProjectList();
	}
	@UiHandler("btnAddStaff")
	void onBtnAddStaffClick(ClickEvent event) {
		HMEntrypoint.get().HMStaffForAddProject();
	}
	@UiHandler("btnSpecialize")
	void onBtnSpecializeClick(ClickEvent event) {
		HMEntrypoint.get().HMSkillList();
	}
	@UiHandler("btnMess")
	void onBtnMessClick(ClickEvent event) {
		HMEntrypoint.get().HMMessList();
	}
	@UiHandler("btnGeneralInfo")
	void onBtnGeneralInfoClick(ClickEvent event) {
		HMEntrypoint.get().HMHomepage();
	}
	@UiHandler("btnHmRequire")
	void onBtnHmRequireClick(ClickEvent event) {
		HMEntrypoint.get().HMHumanReq();
	}
	@UiHandler("hypSingOut")
	void onHypSingOutClick(ClickEvent event) {
		loginService.logout(new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				Window.Location.reload();
			}

			@Override
			public void onFailure(Throwable caught) {
				//Window.alert("RPC có lỗi ");
			}
		});
	}
	public void getUserByID() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {
			
			@Override
			public void onSuccess(UsersDTO result) {
				userDTO=result;
				lblFullName.setText("Xin chào: " + result.getFullName());
			}
			
			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}
	@UiHandler("btnChangePass")
	void onBtnChangePassClick(ClickEvent event) {
		ChangePassword dialog=new ChangePassword(userDTO);
		dialog.setPopupPosition(350, 120);
		dialog.show();
	}
}
